#!/usr/bin/env bash

set -o errexit
set -o nounset
set -o xtrace # can remove this later, for now it's nice to see commands

# TODO support subsetting the benchmarks from the command line

if [ "$#" -ne 1 ]; then
    cat << EOM
Expected one command line argument: the name of an initially-empty working
directory for sources, binaries, and reports.
EOM
    exit -1
fi

OS="$(uname -s)"

WORK=$1
REPORTS=$WORK/reports

# TODO give alivecc a mode where it doesn't invoke alive at all, so we
# can test scripts like this more quickly and easily

if [[ "$OS" == "Linux" ]]; then
    CORES="$(nproc)"
elif [[ "$OS" == "Darwin" ]]; then
    CORES="$(sysctl -n hw.logicalcpu)"
else # default
    CORES=1
fi

ALIVECC=@PROJECT_BINARY_DIR@/alivecc

export ALIVECC_OVERWRITE_REPORTS=1
export ALIVECC_SUBPROCESS_TIMEOUT=600
export ALIVECC_SMT_TO=30000
export ALIVECC_DISABLE_UNDEF_INPUT=1
export ALIVECC_PARALLEL_UNRESTRICTED=1

mkdir $WORK || true
cd $WORK
mkdir downloads
mkdir sources

function check_bzip {
    # 16 minutes on John's 10-core machine
    export ALIVECC_REPORT_DIR=$REPORTS/bzip
    cd $WORK/downloads
    wget -nc https://sourceware.org/pub/bzip2/bzip2-1.0.8.tar.gz
    cd ../sources
    tar xvf ../downloads/bzip2-1.0.8.tar.gz
    cd bzip2-1.0.8
    time make -j$CORES CC=$ALIVECC &> make.out
}

function check_gzip {
    # 16 minutes on John's 10-core machine
    export ALIVECC_REPORT_DIR=$REPORTS/gzip
    cd $WORK/downloads
    wget -nc https://ftp.gnu.org/gnu/gzip/gzip-1.10.tar.xz
    cd ../sources
    tar xvf ../downloads/gzip-1.10.tar.xz
    cd gzip-1.10
    mkdir build
    cd build
    ../configure
    time make -j$CORES CC=$ALIVECC &> make.out
}

function check_libogg {
    # 33 minutes on John's 10-core machine
    export ALIVECC_REPORT_DIR=$REPORTS/libogg
    cd $WORK/downloads
    wget -nc https://downloads.xiph.org/releases/ogg/libogg-1.3.4.tar.xz
    cd ../sources
    tar xvf ../downloads/libogg-1.3.4.tar.xz
    cd libogg-1.3.4
    mkdir build
    cd build
    ../configure
    time make -j$CORES CC=$ALIVECC &> make.out
}

function check_ph7 {
    # doesn't finish within 12 hours on John's machine -- we need to parallelize better!
    export ALIVECC_REPORT_DIR=$REPORTS/ph7
    cd $WORK/downloads
    wget -nc http://www.symisc.net/downloads/ph7-amalgamation-2001004.zip
    cd ../sources
    mkdir ph7
    cd ph7
    unzip ../../downloads/ph7-amalgamation-2001004.zip
    time $ALIVECC -O3 -c ph7.c &> make.out
}

function check_sqlite {
    # doesn't finish within 24 hours on John's machine -- we need to parallelize better!
    export ALIVECC_REPORT_DIR=$REPORTS/sqlite
    cd $WORK/downloads
    wget -nc https://www.sqlite.org/2020/sqlite-amalgamation-3340000.zip
    cd ../sources
    unzip ../downloads/sqlite-amalgamation-3340000.zip
    cd sqlite-amalgamation-3340000
    time $ALIVECC -O3 -c sqlite3.c &> make.out
}

function check_embench {
    # 27.5 minutes on John's machine
    export ALIVECC_REPORT_DIR=$REPORTS/embench
    cd $WORK/sources
    # FIXME -- grab a release, once they make one
    git clone https://github.com/embench/embench-iot
    cd embench-iot
    time ./build_all.py --clean --arch native --chip default --board default --cc alivecc --timeout 999999 &> make.out
}

function check_musl {
    # 57 minutes on John's machine
    export ALIVECC_REPORT_DIR=$REPORTS/musl
    cd $WORK/downloads
    wget -nc https://musl.libc.org/releases/musl-1.2.1.tar.gz
    cd ../sources
    tar xvf ../downloads/musl-1.2.1.tar.gz
    cd musl-1.2.1
    mkdir build
    cd build
    ../configure CC=alivecc
    time make -j$CORES &> make.out
}

check_bzip
check_gzip
check_libogg
#check_ph7
#check_sqlite
check_embench
check_musl
